package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Remark;
import com.xxx.entity.dto.RemarkDto;
import com.xxx.service.IRemarkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/remark")
@Tag(name = "评论管理")
public class RemarkController {

    @Autowired
    private IRemarkService remarkService;

    @PostMapping("/add")
    @Operation(summary = "添加评论")
    public Result add(@RequestBody Remark remark){
        Boolean flag = remarkService.save(remark);
        return flag ? Result.success("添加评论成功") : Result.error("添加评论失败");
    }

    @GetMapping("/delete/{remarkId}")
    @Operation(summary = "删除评论")
    public Result delete(@PathVariable Integer remarkId){
        Boolean flag = remarkService.removeById(remarkId);
        return flag ? Result.success("删除评论成功") : Result.error("不存在该评论Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改评论")
    public Result update(@RequestBody Remark remark){
        Boolean flag = remarkService.updateById(remark);
        return flag ? Result.success("修改评论成功") : Result.error("修改评论失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询评论")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Remark> page = remarkService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据评论Id、用户Id、评论状态、评论父亲条件查询评论")
    public Result selectByCondition(@RequestBody RemarkDto remarkDto){
        LambdaQueryWrapper<Remark> lqw = new LambdaQueryWrapper<>();
        lqw.eq(remarkDto.getUserId()!=null,Remark::getUserId,remarkDto.getUserId())
                .eq(remarkDto.getRemarkStatus()!=null,Remark::getRemarkStatus,remarkDto.getRemarkStatus())
                .eq(remarkDto.getRemarkFather()!=null,Remark::getRemarkFather,remarkDto.getRemarkFather())
                .eq(remarkDto.getAnotherWho()!=null&&!remarkDto.getAnotherWho().equals(""),
                        Remark::getAnotherWho,remarkDto.getAnotherWho())
                .eq(remarkDto.getAnotherId()!=null,Remark::getAnotherId,remarkDto.getAnotherId())
                .eq(remarkDto.getRemarkId()!=null,Remark::getRemarkId,remarkDto.getRemarkId());
        IPage<Remark> page = remarkService.page(new Page<>(remarkDto.getCurrent(), remarkDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

